package com.jztchat.redis;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.jztchat.dataobject.MessageInfo;
import com.jztchat.entity.ChatRecord;
import com.jztchat.entity.ClientInfo;
import com.jztchat.repository.ChatRecordRepository;
import com.jztchat.repository.ClientInfoRepository;
import com.jztchat.util.ApplicationContextUtil;
import com.jztchat.util.JsonUtil;
import com.jztchat.webSocket.service.SocketConnectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.security.crypto.codec.Utf8;

/**
 * Created by zzy on 2018/1/30.
 */
public class RedisListenHandler implements MessageListener {


    private final SocketIOServer server;

    @Autowired
    public RedisListenHandler(SocketIOServer server)
    {
        this.server = server;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String json = Utf8.decode(message.getBody());
        ChatRecord chatRecord=JsonUtil.Json2Object(json,ChatRecord.class);
        SocketConnectService socketConnectService=ApplicationContextUtil.getBean(SocketConnectService.class);
        socketConnectService.sendTheMessageBefore(chatRecord,server);
    }
}
